<template>
  <div class="register-page">
    <div class="register-container">
      <h1>注册医疗众包平台</h1>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
      
      <form @submit.prevent="register">
        <div class="form-group">
          <label for="username">用户名 *</label>
          <input 
            type="text" 
            id="username" 
            v-model="userData.username" 
            required
            placeholder="请输入用户名"
          >
        </div>
        
        <div class="form-group">
          <label for="email">电子邮箱 *</label>
          <input 
            type="email" 
            id="email" 
            v-model="userData.email" 
            required
            placeholder="请输入电子邮箱"
          >
        </div>
        
        <div class="form-group">
          <label for="password">密码 *</label>
          <input 
            type="password" 
            id="password" 
            v-model="userData.password" 
            required
            placeholder="请输入密码"
          >
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码 *</label>
          <input 
            type="password" 
            id="confirmPassword" 
            v-model="confirmPassword" 
            required
            placeholder="请再次输入密码"
          >
          <div v-if="passwordMismatch" class="validation-error">
            两次输入的密码不一致
          </div>
        </div>
        
        <div class="form-group">
          <label for="role">角色 *</label>
          <select id="role" v-model="userData.role" required>
            <option value="" disabled>请选择角色</option>
            <option :value="1">研究者</option>
            <option :value="2">数据贡献者</option>
          </select>
        </div>
        
        <div class="form-group">
          <label for="walletAddress">区块链钱包地址</label>
          <input 
            type="text" 
            id="walletAddress" 
            v-model="userData.walletAddress" 
            placeholder="0x..."
          >
          <div class="help-text">
            可选。如有以太坊钱包地址，请填写，以便接收项目奖励。
          </div>
        </div>
        
        <div class="form-actions">
          <button type="submit" class="btn btn-primary" :disabled="loading || passwordMismatch">
            {{ loading ? '注册中...' : '注册' }}
          </button>
          <router-link to="/login" class="login-link">
            已有账号？立即登录
          </router-link>
        </div>
      </form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Register',
  data() {
    return {
      userData: {
        username: '',
        email: '',
        password: '',
        role: '',
        walletAddress: ''
      },
      confirmPassword: '',
      loading: false,
      error: null
    }
  },
  computed: {
    passwordMismatch() {
      return this.userData.password && 
             this.confirmPassword && 
             this.userData.password !== this.confirmPassword
    }
  },
  methods: {
    async register() {
      if (this.passwordMismatch) {
        return
      }
      
      this.loading = true
      this.error = null
      
      try {
        // 调用Vuex中的auth模块的register方法
        await this.$store.dispatch('auth/register', this.userData)
        
        // 注册成功，跳转到登录页
        this.$router.push({ 
          name: 'login',
          params: { 
            message: '注册成功，请登录' 
          }
        })
      } catch (error) {
        this.error = error.response?.data?.message || '注册失败，请稍后再试'
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style scoped>
.register-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 80vh;
  padding: 20px 0;
}

.register-container {
  width: 100%;
  max-width: 500px;
  padding: 30px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

h1 {
  text-align: center;
  margin-bottom: 24px;
  color: #2c3e50;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #2c3e50;
}

input, select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
}

select {
  background-color: white;
  height: 42px;
}

.help-text {
  margin-top: 5px;
  font-size: 12px;
  color: #7f8c8d;
}

.form-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.btn {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  margin-bottom: 12px;
}

.btn-primary {
  background-color: #3498db;
  color: white;
}

.btn-primary:hover {
  background-color: #2980b9;
}

.btn:disabled {
  background-color: #95a5a6;
  cursor: not-allowed;
}

.login-link {
  color: #3498db;
  text-decoration: none;
  font-size: 14px;
}

.login-link:hover {
  text-decoration: underline;
}

.error-message {
  background-color: #f8d7da;
  color: #721c24;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.validation-error {
  color: #e74c3c;
  font-size: 14px;
  margin-top: 5px;
}
</style> 